package main

import (
	"io/ioutil"
	"net/http"
	"net/http/httputil"
	"net/url"
)

type MiddleResponseWriter struct {
	writer http.ResponseWriter
}

type Server struct {
	*httputil.ReverseProxy
}

func (m MiddleResponseWriter) Header() http.Header {
	return m.writer.Header()
}
func (m MiddleResponseWriter) Write(data []byte) (int, error) {
	println("write data:")
	println("----------------")
	println(string(data))
	println("----------------")
	return m.writer.Write(data)
}
func (m MiddleResponseWriter) WriteHeader(statusCode int) {
	println("status code:", statusCode)
	m.writer.WriteHeader(statusCode)
}
func (m Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	println("========Request=======")
	println("URL:", req.URL.String())
	println("Method", req.Method)
	b, er := ioutil.ReadAll(req.Body)
	println("Request Body:", er, string(b))
	println("========End=======")
	m.ReverseProxy.ServeHTTP(MiddleResponseWriter{rw}, req)
}

func main() {
	//instance:=EurekaClientGo.InstanceInfo{}
	//instance.InitDefaults()
	//instance.Port=9928
	//instance.AppName="EurekaGoTest"
	//instance.InstanceId="EUREKAGO:localhost:9928"
	//client:=EurekaClientGo.EurekaClient{Cron:nil,ServiceUrl: "http://root:root@localhost:9088/eureka/",InstanceInfo:instance}
	u, _ := url.Parse("http://127.0.0.1:9000")
	proxy := httputil.NewSingleHostReverseProxy(u)
	m := Server{proxy}
	http.ListenAndServe(":9188", m)
	//client.Register()
}
